package chapter14;

/**
 * @program: exercise
 * @description: 二分查找算法
 * @author: 陈庆彪
 * @create: 2020-03-09 13:02
 **/
public class BinarySearch {
    public static void main(String[] args) {
        int[] list = {2, 4, 7, 11, 15, 22, 33, 36, 39, 45};
        int t = binarySearch(list, 33);
        System.out.println(t);
    }

    private static int binarySearch(int[] list, int key) {
        int low = 0;
        int high = list.length - 1;
        while (high >= low) {
            int mid = (low + high) / 2;
            if (key < list[mid])
                high = mid - 1;
            else if (key == list[mid])
                return mid;
            else
                low = mid + 1;
        }

        return -low - 1;
    }
//    public static void main(String[] args) {
//        double[] list = {1, 9, 4.5, 6.6, 5.7, -4.5};
//        BinarySearch.selectionSort(list);
//    }
//    private static void selectionSort(double[] list) {
//        for (int i = 0; i <list.length-1 ; i++) {
//            double currentMin=list[i];
//            int currentMinIndex=i;
//            for (int j = i+1; j <list.length-1 ; j++) {
//                if (currentMin>list[j]){
//                    currentMin=list[j];
//                    currentMinIndex=j;
//                }
//            }
//            if (currentMinIndex!=i){
//                list[currentMinIndex]=list[i];
//                list[i]=currentMin;
//            }
//        }
//    }
}
